package com.flykoc.lightinggem;

import com.flykoc.lightinggem.command.CommandHandler;
import com.flykoc.lightinggem.dao.GemDao;
import com.flykoc.lightinggem.dto.LightingGem;
import com.flykoc.lightinggem.listener.EntityDamageByEntityListener;
import com.flykoc.lightinggem.listener.EntityDeathListener;
import com.flykoc.lightinggem.listener.FurnaceBurnListener;
import com.flykoc.lightinggem.listener.FurnaceSmeltListener;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;
import java.util.Set;

/**
 * 插件主类
 *
 * @author Zoyn
 * @since 2017-10-21
 */
public class Entry extends JavaPlugin {

    private static Entry instance;
    public List<Integer> weaponIds;

    @Override
    @SuppressWarnings({"deprecation"})
    public void onEnable() {
        instance = this;
        register();

        saveDefaultConfig();
        loadGems();
        // 读取可用物品的id
        weaponIds = getConfig().getIntegerList("weapons");

        // 注册熔炼
        for (int i = 0; i < weaponIds.size(); i++) {
            FurnaceRecipe Furnace = new FurnaceRecipe(new ItemStack(Material.getMaterial(weaponIds.get(i))), Material.COAL);
            Furnace.setInput(Material.getMaterial(weaponIds.get(i)));
            getServer().addRecipe(Furnace);
        }

    }

    public void loadGems() {
        Set<String> gemNameSet = getConfig().getConfigurationSection("gems").getKeys(false);

        // 数据缓冲
        int material;
        int data;
        String displayName;
        int level;
        List<String> description;
        double damage;
        double odds;
        LightingGem gem;

        // 宝石加载
        ConfigurationSection gems = getConfig().getConfigurationSection("gems");
        for (String keyName : gemNameSet) {
            material = gems.getInt(keyName + ".material");
            data = gems.getInt(keyName + ".data");
            displayName = ChatColor.translateAlternateColorCodes('&', gems.getString(keyName + ".name"));
            level = gems.getInt(keyName + ".level");
            description = gems.getStringList(keyName + ".description");
            damage = gems.getDouble(keyName + ".damage");
            odds = gems.getDouble(keyName + ".odds");

            // 宝石对象实例化
            gem = new LightingGem(material, data, damage, displayName, level, description, odds);
            GemDao.putGem(keyName, gem);
            Bukkit.getConsoleSender().sendMessage("§8[§bLightingGem§8]§6 add gem " + displayName + "§6 successfully!");
        }
    }

    /**
     * 获取主类实例
     *
     * @return {@link Entry}
     */
    public static Entry getInstance() {
        return instance;
    }

    private void register() {
        Bukkit.getPluginCommand("sdbs").setExecutor(new CommandHandler());
        Bukkit.getPluginManager().registerEvents(new EntityDamageByEntityListener(), this);
        Bukkit.getPluginManager().registerEvents(new EntityDeathListener(), this);
        Bukkit.getPluginManager().registerEvents(new FurnaceBurnListener(), this);
        Bukkit.getPluginManager().registerEvents(new FurnaceSmeltListener(), this);
    }
}
